package com.mf.controller.pageConfig;

import com.mf.entity.bo.pageConfig.BasicPageItemBo;
import com.mf.entity.request.pageConfig.PageItemAddReq;
import com.mf.entity.vo.PageItemVo;
import com.mf.service.pageConfig.IBasicPageItemService;
import io.github.heollhai.common.result.Result;
import io.github.heollhai.common.utils.VerifyUtils;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * @author mf
 * @since 2024-12-19
 */
@RestController
@RequestMapping("basic/pageItem/v1")
public class BasicPageItemController {
    @Resource
    private IBasicPageItemService pageItemService;

    @Operation(summary = "新增|删除")
    @PostMapping("save")
    public Result<Boolean> save(@Valid @RequestBody() PageItemAddReq pageItemAddReq) {
        BasicPageItemBo pageItemBo = new BasicPageItemBo();
        BeanUtils.copyProperties(pageItemAddReq, pageItemBo);
        if (VerifyUtils.isEmpty(pageItemAddReq.getId())) {
            pageItemService.save(pageItemBo);
        }
        return Result.success(pageItemService.updateById(pageItemBo));
    }

    @Operation(summary = "删除")
    @GetMapping("delete/{id}")
    public Result<Boolean> deleteById(@PathVariable("id") Integer id) {
        return Result.success(pageItemService.removeById(id));
    }

    @Operation(summary = "查询")
    @GetMapping("findByPageId/{pageId}")
    public Result<List<PageItemVo>> findByPageId(@PathVariable("pageId") Integer pageId) {
        return Result.success(pageItemService.findByPageId(pageId));
    }
}
